using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Mvc;
using System;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace DevExpress.Web.Demos {
    public partial class ExportingController : DemoController {
        public ActionResult Export() {
            return DemoView("Export", NorthwindDataProvider.GetInvoices());
        }
        public ActionResult ExportPartial() {
            return PartialView("ExportPartial", NorthwindDataProvider.GetInvoices());
        }
        public ActionResult ExportTo() {
            foreach(string typeName in GridViewExportDemoHelper.ExportTypes.Keys) {
                if(Request.Params[typeName] != null)
                    return GridViewExportDemoHelper.ExportTypes[typeName].Method(GridViewExportDemoHelper.ExportGridViewSettings, NorthwindDataProvider.GetInvoices());
            }
            return RedirectToAction("Export");
        }
    }

    public partial class GridViewExportDemoHelper {
        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings {
            get {
                if(exportGridViewSettings == null)
                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }
        static GridViewSettings CreateExportGridViewSettings() {
            GridViewSettings settings = new GridViewSettings();

            settings.Name = "gvExport";
            settings.CallbackRouteValues = new { Controller = "Exporting", Action = "ExportPartial" };
            settings.Width = Unit.Percentage(100);

            settings.Columns.Add("CompanyName");
            settings.Columns.Add("City").GroupIndex = 1;
            settings.Columns.Add("Country").GroupIndex = 0;
            settings.Columns.Add("UnitPrice").PropertiesEdit.DisplayFormatString = "c";
            settings.Columns.Add("Quantity");
            
            MVCxGridViewColumn column = settings.Columns.Add("Total");
            column.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            column.PropertiesEdit.DisplayFormatString = "c";
            settings.CustomUnboundColumnData = (sender, e) => {
                if(e.Column.FieldName == "Total") {
                    decimal price = (decimal)e.GetListSourceFieldValue("UnitPrice");
                    int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
                    e.Value = price * quantity;
                }
            };

            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "CompanyName");
            settings.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "Total");
            settings.Settings.ShowFooter = true;
            settings.SettingsExport.RenderBrick = (sender, e) => {
                if(e.RowType == GridViewRowType.Data && e.VisibleIndex % 2 == 0)
                    e.BrickStyle.BackColor = System.Drawing.Color.FromArgb(0xEE, 0xEE, 0xEE);
            };

            settings.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "Total");
            settings.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "CompanyName");
            settings.Settings.ShowGroupPanel = true;
            return settings;
        }
    }
}
